<?php
class Mysql{ 

  //jquery查询数量
  var $querycount = 0;

  var $db;
   
  function __construct (){
	$num_args = func_num_args();		  
	if($num_args > 0){
		$args = func_get_args();
		$this->host = $args[0];
		$this->user = $args[1];
		$this->pass = $args[2];
		$this->connect();
	}
  }
	
  /**
   *连接数据库
   */
  function connect(){

    if (!$this->db = @mysql_connect ($this->host,$this->user,$this->pass)){
	   $this->showerror('数据库连接错误');
	}

	if($this->getdbversion()>'5.0.1')
		mysql_query("SET sql_mode=''");
  }

  /**
   *选择数据库
   */
  function selectdb ($thedb){
	
    if (!@mysql_select_db ($thedb, $this->db)){
	    $this->showerror('数据库表连接错误');
     }
  }

  /**
   *发送查询语句
   */
  function query($thequery){

    if (!@ mysql_query ($thequery, $this->db)){
	   $this->showerror('数据查询失败');
    }
	$this->querycount++;
  }

  /**
   *连接编码
   */
  function setchar($chartype){
     $this->query("SET NAMES $chartype");
  }

  /**
   *返回上一部操作ID
   */
 function insert_id(){
	 return @mysql_insert_id($this->db);
 }

  /**
   *释放内存
   */
  function free($queryr){
	  return @mysql_free_result($queryr);
  }

  /**
   *获取字段
   */
  function getthefield($table){
      $fields=mysql_list_fields(DB_NAME,DB_PREFIX.$table,$this->db);
	  $columns  = @mysql_num_fields($fields); 
      $a=array();
      for ($i=0;$i<$columns;$i++){ 
          $a[]= @mysql_field_name($fields,$i); 		
      } 
	  return $a;
  }

  /**
   *返回记录总数
   */
  function getresultnum($thequery){
   if (!$resultquery = @mysql_query ($thequery)){
	 $this->showerror('数据库查询失败');
   }else{
	 $r=mysql_fetch_array($resultquery);
	 $this->free($resultquery);
	 return $r['0'];												
   }
 }
  
  /**
   *返回一维数组
   */
  function getonerow ($thequery){
    if (!$resultquery = @mysql_query ($thequery)){
		$this->showerror('数据库查询失败');
    }else{
		$returnarray = array ();
		while ($row = @mysql_fetch_assoc ($resultquery)){
			$returnarray = array_merge ($returnarray,$row);
		}
		$this->free($resultquery);
		return $returnarray;
   } 
 }
		
 /**
   *返回二维数组
   */	
 function gettworow($thequery){
  if (!$resultquery = @mysql_query ($thequery)){
	$this->showerror('数据查询失败');
  }else{
	$returnarray = array ();
	$i=0;
	while ($row = @mysql_fetch_assoc ($resultquery)){
		$returnarray[$i]=$row;
		$i++;
	}
	$this->free($resultquery);
	return $returnarray;
 }
}

 /**
   *获取错误
   */
 function showerror($errortype){
    $errorstring= @mysql_errno() . ": " . @mysql_error()."<br>".$errortype;
	echo($errorstring);
	exit();
}

/**
  *关闭数据库
  */
function __destruct(){

 if (!@mysql_close ($this->db)){   
	$this->showerror('数据库关闭错误');
  }
 }

 public function getdbversion($db=null){
	 if($db===null)
		 $db=$this->db;
	$sReallyVersion = mysql_get_server_info( $db );

		// 获取数据库版本< 数字 >
		if( !is_numeric( $sReallyVersion) ){

			$arrVar = explode( '-' ,$sReallyVersion );
			$nVersion = array_shift( $arrVar );
		}

		else{

			$nVersion = $sReallyVersion;
		}
		return $nVersion;
 }
}
	
?>